package com.tongda.core.utils;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tongda.core.repository.MenuDishDao;
import com.tongda.core.vo.Dish;
import com.tongda.core.vo.DishesPic;
import com.tongda.core.vo.DishesSetDishes;
import com.tongda.core.vo.DishesSetDishesReplace;
import com.tongda.core.vo.SpecialDishe;

import android.content.Context;
import android.widget.ArrayAdapter;

public class DishUtil {

	private static DishUtil instance;
	private MenuDishDao dish_dao;

	public DishUtil(Context context) {
		dish_dao = MenuDishDao.getInstance(context);
	}

	public static synchronized DishUtil getInstance(Context context) {
		if (instance == null) {
			instance = new DishUtil(context);
		}
		return instance;
	}

	/*
	 * 将所有菜品中的菜名取出，并转换成数组以方便AutoTextAdapter调用
	 */
	public String[] switch2Arrays(List<Dish> dishes) {
		String[] names = new String[dishes.size()];
		for (int i = 0; i < dishes.size(); i++) {
			names[i] = dishes.get(i).getDishesName();
		}
		return names;
	}

	/*
	 * MainActivity中初始化默认菜品
	 */
	public List<Dish> initDish(String dishCategoryId) {
		// 菜类中加入“全部”之后的初始化菜肴，只要初始化“全部”分类即可
		List<Dish> initDish = dish_dao.getDishesByParams(null);
		return initDish;
	}

	/*
	 * MainActivity根据单击项目获取相关菜肴
	 */
	public List<Dish> getTheDish(String dishCategoryId) {
		List<Dish> initDish = new ArrayList<Dish>();
		if (dishCategoryId.equals("all")) {
			initDish = dish_dao.getDishesByParams(null);
		} else if (dishCategoryId.equals("zpc")) {
			initDish = dish_dao.get4Dishes("isSpecialty");
		} else if (dishCategoryId.equals("tjc")) {
			initDish = dish_dao.get4Dishes("isRecommend");
		} else if (dishCategoryId.equals("tjcy")) {
			initDish = dish_dao.get4Dishes("specialPrice");
		} else if (dishCategoryId.equals("hytj")) {
			initDish = dish_dao.get4Dishes("memberPrice");
		} else {
			Map<String, String> params4Category = new WeakHashMap<String, String>();
			// 2.2.0之后的新需求，如果一级菜类下面没有菜就把归属它的二级菜类的菜显示出来
			List<String> subCategoryIds = dish_dao
					.getsubCategoryId(dishCategoryId);
			if (subCategoryIds.size() != 0) {
				for (String subId : subCategoryIds) {
					params4Category.put("categoryId", subId);
					initDish.addAll(dish_dao.getDishesByParams(params4Category));
				}
			} else {
				params4Category.put("categoryId", dishCategoryId);
				initDish = dish_dao.getDishesByParams(params4Category);
			}

		}
		return initDish;
	}

	/*
	 * SplashActivity中调用，将List<DishesSetDishes>转化成json字符串
	 */
	public String dsDishes2JsonStr(List<DishesSetDishes> dsDishes) {
		JSONArray dsDishesArray = new JSONArray();
		if (dsDishes != null && dsDishes.size() > 0 && !dsDishes.contains(null)) {
			for (DishesSetDishes dsDish : dsDishes) {
				JSONObject dsDishObject = new JSONObject();
				try {
					dsDishObject.put("dsDishesId", dsDish.getDsDishesId());
					dsDishObject.put("mr_dishesId", dsDish.getMr_dishesId());
					dsDishObject
							.put("mr_dishesName", dsDish.getMr_dishesName());
					dsDishObject
							.put("mr_dishesCode", dsDish.getMr_dishesCode());
					dsDishObject.put("mr_unitNum", dsDish.getMr_unitNum());
					dsDishObject.put("mr_unitName", dsDish.getMr_unitName());
					dsDishObject.put("dishesId", dsDish.getDishesId());
					dsDishObject.put("dishesName", dsDish.getDishesName());
					dsDishObject.put("dishesCode", dsDish.getDishesCode());
					dsDishObject.put("unitNum", dsDish.getUnitNum());
					dsDishObject.put("unitName", dsDish.getUnitName());
					// 处理可替换菜肴
					List<DishesSetDishesReplace> dsReplaces = dsDish
							.getDishesSetDishesReplaces();
					JSONArray dsReplaceArray = new JSONArray();
					for (DishesSetDishesReplace dsReplace : dsReplaces) {
						JSONObject dsObject = new JSONObject();
						dsObject.put("dishesId", dsReplace.getDishesId());
						dsObject.put("dishesName", dsReplace.getDishesName());
						dsObject.put("dishesCode", dsReplace.getDishesCode());
						dsObject.put("dsUnitNum", dsReplace.getDsUnitNum());
						dsObject.put("dishesUnitName",
								dsReplace.getDishesUnitName());
						dsReplaceArray.put(dsObject);
					}
					dsDishObject.put("dishesSetDishesReplaces",
							dsReplaceArray.toString());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				dsDishesArray.put(dsDishObject);
			}
		}
		return dsDishesArray.toString();
	}

	/*
	 * SplashActivity中调用，将List<DishesPic>的网址取出，并转化成json字符串
	 */
	public String pics2JsonStr(List<DishesPic> pics) {
		JSONArray picArrays = new JSONArray();
		if (pics != null && pics.size() > 0 && !pics.contains(null)) {
			for (int j = 0; j < pics.size(); j++) {
				JSONObject picObject = new JSONObject();
				String url = pics.get(j).getPicUrl();
				try {
					picObject.put("picUrl", url);
					picArrays.put(picObject);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		return picArrays.toString();
	}

	/*
	 * 将List<SpecialDishe>转化成json字符串
	 */
	public String spDishes2Str(List<SpecialDishe> spDishes) {
		JSONArray spDishesArray = new JSONArray();
		if (spDishes != null && spDishes.size() > 0) {
			for (int i = 0; i < spDishes.size(); i++) {
				JSONObject object = new JSONObject();
				SpecialDishe spDish = spDishes.get(i);
				try {
					object.put("spDateStr", spDish.getSpDateStr());
					object.put("spTime", spDish.getSpTime());
					object.put("specialPrice", spDish.getSpecialPrice());
					object.put("spiName", spDish.getSpiName());
					spDishesArray.put(object);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		return spDishesArray.toString();
	}

	/*
	 * SplashActivity中调用，将List<DishesPic>的低分辨率网址取出，并转化成json字符串
	 */
	public String lowPics2JsonStr(List<DishesPic> pics) {
		JSONArray picArrays = new JSONArray();
		if (pics != null && pics.size() > 0 && !pics.contains(null)) {
			for (int j = 0; j < pics.size(); j++) {
				JSONObject picObject = new JSONObject();
				String url = pics.get(j).getPiclowUrl();
				try {
					picObject.put("piclowUrl", url);
					picArrays.put(picObject);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		return picArrays.toString();
	}

	public static String beanToJson(Object obj) {
		String json = null;
		try {
			ObjectMapper mapper = new ObjectMapper();
			StringWriter writer = new StringWriter();
			JsonGenerator generator = new JsonFactory()
					.createJsonGenerator(writer);
			mapper.writeValue(generator, obj);
			generator.close();
			json = writer.toString();
			writer.close();
			JSONArray arr = new JSONArray();
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}

	/*
	 * CartActivity中调用，用来更换套餐的菜肴
	 */
	public String dsDishes2Json(List<DishesSetDishes> dsDishes) {
		JSONArray dsDishesArray = new JSONArray();
		if (dsDishes != null && dsDishes.size() > 0) {
			for (DishesSetDishes dsDish : dsDishes) {
				JSONObject dsDishObject = new JSONObject();
				try {
					dsDishObject.put("dsDishesId", dsDish.getDsDishesId());
					dsDishObject.put("mr_dishesId", dsDish.getMr_dishesId());
					dsDishObject
							.put("mr_dishesName", dsDish.getMr_dishesName());
					dsDishObject
							.put("mr_dishesCode", dsDish.getMr_dishesCode());
					dsDishObject.put("mr_unitNum", dsDish.getMr_unitNum());
					dsDishObject.put("mr_unitName", dsDish.getMr_unitName());
					dsDishObject.put("dishesId", dsDish.getDishesId());
					dsDishObject.put("dishesName", dsDish.getDishesName());
					dsDishObject.put("dishesCode", dsDish.getDishesCode());
					dsDishObject.put("unitNum", dsDish.getUnitNum());
					dsDishObject.put("unitName", dsDish.getUnitName());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				dsDishesArray.put(dsDishObject);
			}
		}
		return dsDishesArray.toString();
	}
}
